package com.sky.course.controller;

import com.sky.course.entity.Course;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.util.List;

@RestController
@RequestMapping(value = "/consumer")
public class CourseController_Consumer {

    //private static final String REST_URL_PREFIX = "http://localhost:8001";
    private static final String REST_URL_PREFIX = "http://COURSE";

    /**
     * 使用RestTemplate访问restful接口非常简单粗暴无脑
     * （url，requestMap,responseBean.class）参数分别是请求地址，参数，Http响应转换成对象类型
     */
    @Autowired
    private RestTemplate restTemplate;

    @RequestMapping(value = "/course/add")
    public boolean add(Course course){
        return restTemplate.postForObject(REST_URL_PREFIX+"/course/add",course,Boolean.class);
    }

    @RequestMapping(value = "/course/get/{cid}")
    public Course get(@PathVariable("cid") Integer cid){
        System.out.println("--------------"+REST_URL_PREFIX);
        return restTemplate.getForObject(REST_URL_PREFIX+"/course/get/"+cid,Course.class);
    }

    @RequestMapping(value = "/course/list")
    public List<Course> list(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/course/list",List.class);
    }
    //测试
    @RequestMapping(value = "/course/discovery")
    public Object discovery(){
        return restTemplate.getForObject(REST_URL_PREFIX+"/course/discovery",Object.class);
    }
}
